using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Toenda.Rails.System;
using Toenda.Rails.DataContainer;

public partial class Controls_Main_ctrl_addtoset : System.Web.UI.UserControl {
	private AccountProvider ap = new AccountProvider();
	private ImageProvider ip = new ImageProvider();

	/// <summary>
	/// Page_Load
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	protected void Page_Load(object sender, EventArgs e) {
		bool canCreate = false;

		if(Session.Count > 0 && ap.CheckSessionExist(Session["Zappr"].ToString())) {
			if(Request.QueryString.Get("photo") != null) {
				ImageItem item = ip.GetImage(Convert.ToInt64(Request.QueryString.Get("photo")));
				Account tmp = ap.GetAccountBySession(Session["Zappr"].ToString());
				Account acc = ap.GetAccount(item.UserID);

				if(item.UserID == tmp.ID) {
					imgEdit.ImageUrl = "~/Images/" + item.Image240;

					int amount = ip.GetUserSetsAmount(acc.ID);

					// check set amount
					if(acc.Type == 0) {
						if(amount < 3) {
							canCreate = true;
						}
					}
					else {
						canCreate = true;
					}

					if(amount > 0) {
						if(canCreate) {
							HyperLink news = new HyperLink();
							news.CssClass = "normal";
							news.NavigateUrl = "~/default.aspx?id=_createset";
							news.Text = GetLocalResourceObject("CreateSet").ToString();

							plcSet.Controls.Add(news);

							//plcSet.Controls.Add(new LiteralControl("<br />"));
						}

						this.LoadSets(acc, item.ImageID, item.ID);
					}
					else {
						Label stxt = new Label();
						stxt.CssClass = "txtNormal";
						stxt.Text = GetLocalResourceObject("NoSets").ToString();
						stxt.Style.Add("text-align", "left");
						stxt.Style.Add("margin-left", "5px");

						plcSet.Controls.Add(stxt);

						if(canCreate) {
							HyperLink slink = new HyperLink();
							slink.CssClass = "normal";
							slink.NavigateUrl = "~/default.aspx?id=_createset";
							slink.Text = GetLocalResourceObject("CreateSet").ToString();
							slink.Style.Add("text-align", "left");
							slink.Style.Add("margin-left", "5px");

							plcSet.Controls.Add(slink);
						}
					}
				}
				else {
					Response.Redirect("~/default.aspx?item=" + Request.QueryString.Get("photo"));
				}
			}
			else {
				Response.Redirect("~/default.aspx?id=dashboard");
			}
		}
		else {
			Response.Redirect("~/login.aspx");
		}
	}

	/// <summary>
	/// btnAddNow_Click
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	protected void btnAddNow_Click(object sender, CommandEventArgs e) {
		char[] seperator = new char[1];
		seperator[0] = '$';
		string[] spliter = e.CommandArgument.ToString().Split(seperator);

		//this.AddNow(Convert.ToInt64(spliter[0]), Convert.ToInt64(spliter[1]));
		ip.AddImageToSet(spliter[0], spliter[1]);

		Response.Redirect("default.aspx?item=" + spliter[2]);
	}

	/// <summary>
	/// Load sets
	/// </summary>
	/// <param name="puser"></param>
	/// <param name="imageid"></param>
	/// <param name="pkid"></param>
	private void LoadSets(Account puser, long imageid, string pkid) {
		List<SetItem> list = ip.GetUserSets(
			puser.ID,
			false
		);

		if(list != null) {
			if(list.Count > 0) {
				Panel psets = new Panel();

				foreach(SetItem set in list) {
					// thumbnail
					Panel pimg = new Panel();
					pimg.CssClass = "SetCase";

					if(set.Thumbnail != -1) {
						ImageItem mainimg = ip.GetImage(Convert.ToInt64(set.Thumbnail));
						Image thumb = new Image();

						//thumb.NavigateUrl = "~/default.aspx?id=_addtoset&photo=" + imageid.ToString();

						thumb.ImageUrl = "~/Images/" + mainimg.Image75x75;
						thumb.Style.Add("border", "1px solid #eee");
						thumb.Style.Add("position", "absolute");
						thumb.Style.Add("margin-left", "7px");

						pimg.Controls.Add(thumb);
					}

					psets.Controls.Add(pimg);

					// add
					LinkButton add = new LinkButton();
					add.CssClass = "normal";
					//add.OnClientClick = "javascript:chk=confirm('"
					//	+ GetLocalResourceObject("DeleteThis").ToString()
					//	+ "');return chk;";
					add.Text = GetLocalResourceObject("AddNow").ToString();
					add.Text += " (" + set.Name + ")";
					add.Command += new CommandEventHandler(btnAddNow_Click);
					add.CommandName = "btnAddNow_Click";
					add.CommandArgument = pkid + "$" + set.ID + "$" + imageid.ToString();

					psets.Controls.Add(add);

					// space
					//plcSet.Controls.Add(new LiteralControl("<br />"));
				}

				plcSet.Controls.Add(psets);
			}
		}
	}
}
